package com.lz.yz.reward.web;

import com.lz.yz.base.entity.ReturnValue;
import com.lz.yz.reward.dao.RewardMapper;
import com.lz.yz.reward.entity.Reward;
import com.lz.yz.reward.enumeration.RewardStatus;
import com.lz.yz.reward.model.ComplaintsModel;
import com.lz.yz.reward.service.ComplaintsService;
import com.lz.yz.reward.service.RewardService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * ${DESCRIPTION}
 *
 * @author gaoran
 * @create 2017-12-18 15:01
 **/
@RestController
@RequestMapping("/complaints")
public class ComplaintsController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ComplaintsService complaintsService;

    @Resource
    private RewardService rewardService;

    @Autowired
    private RewardMapper rewardMapper;

    @Autowired
    HttpServletRequest request;



    @ApiOperation(value="提交投诉")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer "),
            @ApiImplicitParam(name = "complaintsModel", value = "悬赏实体类", dataType = "ComplaintsModel", paramType = "body"),
    })
    @PostMapping("/addComplaints")
    public ReturnValue addComplaints(@RequestBody ComplaintsModel complaintsModel) {
        try {
            Integer accountId = (Integer) request.getAttribute("accountId");
            Reward reward=new Reward();
            reward=rewardService.findById(complaintsModel.getRewardId());
            reward.setStatus(RewardStatus.COMPLAINTS.getStatusValue());
            rewardMapper.updateByPrimaryKeySelective(reward);//更新找大咖状态
            return complaintsService.add(complaintsModel);
        } catch (Exception e) {
            logger.error("提交投诉出错" + complaintsModel, e);
            return ReturnValue.generateFalseReturnValue("提交投诉失败");
        }
    }
}
